#include "stdafx.h"
#define WINDOWS
#include "exception.h"
#include "HeapImp.h"
#include "Heap.h"

//implementations
using namespace MEM;
using namespace std;


MEM::Heap::Heap( void* ptr,size_t s )
{
	try
	{
		if(s < sizeof(HeapImp))
			throw MemException(MemTooSmall,"Memory is too small for the manager to set up, please increase your memory size.");

		m_heapImp = new (ptr) HeapImp(ptr,s);
	}
	catch(MemException e)
	{
		e.Error();
	}
}

//MEM::Heap::Heap( size_t s )
//{
//	char* memForImp = new char[s];
//
//	try
//	{
//		if(s < sizeof(HeapImp))
//			throw MemException(MemTooSmall,"Memory is too small for the manager to set up, please increase your memory size.");
//
//		m_heapImp = new (memForImp) HeapImp(memForImp,s);
//			
//	}
//	catch(MemException e)
//	{
//		e.Error();
//	}
//}

MEM::Heap::~Heap()
{

}

void* MEM::Heap::Mem_new(size_t s, const char* fileName, int lineNum, int type)
{
	return m_heapImp->Mem_new(s,fileName,lineNum, type);
}

//bool MEM::Heap::Handle( void* ptr, size_t s )
//{
//	return m_heapImp->Handle(ptr,s);
//}


void MEM::Heap::Mem_delete(void* ptr)
{
	m_heapImp->Mem_delete(ptr);
}

Heap* MEM::Heap::subHeap( size_t s )
{
	try
	{
		return new Heap(m_heapImp->Mem_new(s,__FILE__,__LINE__), s);
	}
	catch (MemException e)
	{
		e.Error();
		return nullptr;
	}
}

void MEM::Heap::TotalFreeMem()
{
	m_heapImp->TotalFreeMem();
}

size_t MEM::Heap::LargestFreeBlock()
{
	return m_heapImp->LargestFreeBlock();
}


void MEM::Heap::Verify()
{
	m_heapImp->Verify();
}

void MEM::Heap::Dump()
{
	m_heapImp->Dump();
}

void MEM::Heap::Dump(int type)
{
	m_heapImp->Dump(type);
}

bool MEM::Heap::IsPtrValid( void* ptr )
{
	return  m_heapImp->IsPtrValid(ptr);
}

size_t MEM::Heap::GetWorstCaseUsageMemory()
{
	return m_heapImp->GetWorstCaseUsageMemory();
}

void MEM::Heap::checkLeak()
{
	m_heapImp->checkLeak();
}


